package com.study.demo.ui.thread.collect;

import android.support.annotation.NonNull;

import com.stduy.injector.PerActivity;
import com.study.demo.api.game.GameApi;
import com.study.demo.bean.ThreadListResult;
import com.study.demo.db.CustomThread;
import com.study.demo.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * Created by sll on 2016/3/11.
 */
@PerActivity
public class ThreadCollectPresenter implements CollectThreadListContract.Presenter {

    private GameApi mGameApi;

    private CollectThreadListContract.View mSpecialView;
    private Disposable disposable;
    private List<CustomThread> threads = new ArrayList<>();
    private int page = 1;
    private boolean hasNextPage = true;

    @Inject
    public ThreadCollectPresenter(GameApi gameApi) {
        mGameApi = gameApi;
    }

    @Override
    public void onThreadReceive() {
        mSpecialView.showLoading();
        loadCollectList(page);
    }

    private void loadCollectList(final int page) {
        this.page = page;
        disposable = mGameApi.getCollectList(page)
                .map(threadListData -> {
                    if (page == 1) {
                        threads.clear();
                    }
                    if (threadListData != null && threadListData.result != null) {
                        ThreadListResult data = threadListData.result;
                        hasNextPage = data.nextDataExists == 1;
                        threads.addAll(data.data);
                        return threads;
                    }
                    return null;
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(threadList -> {
                            if (threadList != null) {
                                if (threadList.isEmpty()) {
                                    mSpecialView.onEmpty();
                                } else {
                                    mSpecialView.onLoadCompleted(hasNextPage);
                                    mSpecialView.onRefreshCompleted();
                                    mSpecialView.hideLoading();
                                    mSpecialView.renderThreads(threadList);
                                }
                            } else {
                                loadThreadError();
                            }
                        }, throwable -> loadThreadError()
                );
    }

    private void loadThreadError() {
        if (threads.isEmpty()) {
            mSpecialView.onError("数据加载失败");
        } else {
            mSpecialView.hideLoading();
            mSpecialView.onLoadCompleted(true);
            mSpecialView.onRefreshCompleted();
            ToastUtil.showToast("数据加载失败");
        }
    }

    public void onRefresh() {
        page = 1;
        loadCollectList(page);
    }

    public void onReload() {
        mSpecialView.showLoading();
        loadCollectList(page);
    }

    public void onLoadMore() {
        if (!hasNextPage) {
            ToastUtil.showToast("没有更多了~");
            mSpecialView.onLoadCompleted(false);
            return;
        }
        loadCollectList(++page);
    }

    @Override
    public void attachView(@NonNull CollectThreadListContract.View view) {
        mSpecialView = view;
    }

    @Override
    public void detachView() {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
        mSpecialView = null;
    }
}
